package com.wdz.category.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wdz.article.entity.Article;
import com.wdz.category.entity.Category;
import com.wdz.category.entity.CategoryOTM;
import com.wdz.category.mapper.CategoryMapper;
import com.wdz.category.service.ICategoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wdz.resource.mapper.ResourceMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author 吴传逞
 * @since 2020-12-14
 */
@Service("CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


    @Override
    public List<CategoryOTM> seleteCategorysByOTM() {
        return this.baseMapper.seleteCategorysByOTM();
    }

    @Override
    public List<Map<Object, Object>> selectCategorysByArticleCount(String id) {
        return this.baseMapper.selectCategorysByArticleCount(id);
    }

    @Override
    public List<String> selectCategoryImagesById(String id) {
        return this.baseMapper.selectCategoryImagesById(id);
    }

    @Override
    public Integer selectCategoryLengthById(String id) {
        return this.baseMapper.selectCategoryLengthById(id);
    }

    @Override
    public List<Article> selectCategoryArticlesById(String id) {
        return this.baseMapper.selectCategoryArticlesById(id);
    }

    @Override
    public List<Category> selectCategorysById(String id,Integer pageNo,Integer pageSize) {
        List<Category> list = this.selectPage(new Page<>(pageNo,pageSize),new EntityWrapper<Category>().where("parent_category_id={0}",id)).getRecords();
        for(Category c:list){
            c.setImages(selectCategoryImagesById(c.getId()));
            c.setArticleVolume(selectCategoryLengthById(c.getId()));
        }
        return list;
    }
    private com.wdz.resource.entity.Resource dgRelationCategoryIds(com.wdz.resource.entity.Resource resource){
        if(StringUtils.isEmpty(resource.getRelationCategoryIds())){
            return resource;
        }
        Set<String> ids = new HashSet<>();
        ids.addAll(Arrays.asList(resource.getRelationCategoryIds().split(",")));
        if(!ids.isEmpty()){
            List<Category> categories = this.selectBatchIds(ids);
            resource.setRelationCategorys(categories);
        }
        return resource;
    }

    //根据relation_category_ids查询出relation_category_names
    private List<com.wdz.resource.entity.Resource> dgRelationCategoryIds(List<com.wdz.resource.entity.Resource> datas){
        Set<String> ids = new HashSet<>();
        datas.forEach(item->{
            if(item.getRelationCategoryIds() != null) {
                ids.addAll(Arrays.asList(item.getRelationCategoryIds().split(",")));
            }
        });
        if(!ids.isEmpty()) {
            List<Category> categories = this.selectBatchIds(ids);
            datas.forEach(item1 -> {
                item1.setRelationCategorys(dgRelationCategoryIds(item1, categories));
            });

        }
        return datas;
    }
    private List<Category> dgRelationCategoryIds(com.wdz.resource.entity.Resource resource, List<Category> categories){
        if(resource.getRelationCategoryIds() == null){
            return null;
        }
        String[] ids = resource.getRelationCategoryIds().split(",");
        StringBuilder names = new StringBuilder();
        List<Category> categories1 = new ArrayList<>();
        for (String id : ids) {
            categories.forEach(item->{
                if(id.equals(item.getId())) {
                    Category c = new Category();
                    c.setName(item.getName());
                    c.setId(item.getId());
                    categories1.add(c);
                    c = null;
                }
            });
        }
        return categories1;
    }
    @Override
    public List<Article> selectAllArticlesById(String id,Integer pageNo,Integer pageSize) {
        return this.baseMapper.selectAllArticlesById(id,(pageNo-1)*pageSize,pageSize);
    }

    @Resource
    private ResourceMapper resourceMapper;

    @Override
    public List<com.wdz.resource.entity.Resource> selectAllResourcesById(String id,Integer pageNo,Integer pageSize) {
        return dgRelationCategoryIds(resourceMapper.selectPage(new Page<>(pageNo,pageSize),new EntityWrapper<com.wdz.resource.entity.Resource>().where("parent_category_id={0}",id)));
    }
}
